﻿using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FrontEnd.Application.ContentService;
using FrontEnd.Models;

namespace FrontEnd.Controllers
{
	public class RequestsController : Controller
	{
		public RequestsController(IContentService contentService)
        {
            CodeContract.RequiresAlways(contentService != null);
            this.ContentService = contentService;
        }

        public IContentService ContentService
        {
            get;
            private set;
        }

		public ActionResult Index()
		{
			RequestsModel model =
				new RequestsModel
				{
					Requests = new ListSinglePage<RequestInfo>(this.ContentService.EnumerateAvailableRequests(), 1, 1, 1)
				};

			return View(model);
		}

		public ActionResult IncrementRequestsCount(string requestName)
        {
			int actualRequestCount = this.ContentService.IncrementRequestsCount(requestName);

            return Json(new RequestCountIncreasedResult() { IsSuccess = true, ActualRequestCount = actualRequestCount });
        }

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult CreateNew()
		{
			return View();
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult CreateNew(string title, string description)
		{
			if (string.IsNullOrEmpty(title))
			{
				ModelState.AddModelError("title", "Вы должны ввести хотелки");
			}
			if (string.IsNullOrEmpty(description))
			{
				ModelState.AddModelError("description", "Вы должны ввести краткое описание хотелки");
			}

			if (!ModelState.IsValid)
			{
				return View();
			}

			ContentService.AddRequest(title, description);

			return RedirectToAction("Index");
		}
	}
}
